package com.colabo.j2ee.web.oaf.startup;

import java.util.ArrayList;
import java.util.List;

import com.colabo.j2ee.web.core.cache.AbstractCacheBean;
import com.colabo.j2ee.web.core.cache.CacheManager;


public class CachedInfoManager {
	private static CachedInfoManager ciManager = null;
	private CacheManager cache = new CacheManager();
	private List<AbstractCacheBean> cachedModules = new ArrayList<AbstractCacheBean>();
	private CachedInfoManager(){
		
	}
	
	public static CachedInfoManager getInstance() {
		if (ciManager == null) {
			ciManager = new CachedInfoManager();
		}
		return ciManager;
	}
	
	public void addCacheBean(Class bean) {
		Object instance;
		try {
			instance = bean.newInstance();
			if (instance instanceof AbstractCacheBean) {
				AbstractCacheBean acb = (AbstractCacheBean)instance;
				cachedModules.add(acb);
				acb.addCacheManager(cache);
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void initCachedInfo() {
		for (AbstractCacheBean bean : cachedModules) {
			bean.init();
		}
	}
}
